package com.asktgapp.engineer.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asktgapp.R;
import com.asktgapp.application.ApiUtil;
import com.asktgapp.application.Apiservice;
import com.asktgapp.architecture.retrofit.ApiException;
import com.asktgapp.architecture.retrofit.ApiResponseBody;
import com.asktgapp.architecture.rxjava.ExceptionHandle;
import com.asktgapp.base.BaseFragment;
import com.asktgapp.engineer.activity.FindWithDrawSuccessActivity;
import com.asktgapp.modulebase.common.util.PreferencesUtil;
import com.asktgapp.widget.InputView;
import com.asktgapp.modulebase.common.util.Util;

import java.util.HashMap;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * time:2018/1/18
 * Create By ZhaoKai
 * Email: dczk@foxmail.com
 * Description:
 */

public class SetWithDrawPwdFragment extends BaseFragment {

    @InjectView(R.id.iv_first_eye)
    ImageView mFisrtEyeImg;
    @InjectView(R.id.iv_sencond_eye)
    ImageView mSencondEyeImg;
    @InjectView(R.id.input_first)
    InputView mFirstInputView;
    @InjectView(R.id.input_sencond)
    InputView mSencondInputView;
    @InjectView(R.id.tv_sure)
    TextView mSure;
    @InjectView(R.id.tv_title1)
    TextView mTitle1;
    @InjectView(R.id.tv_title2)
    TextView mTitle2;
    private String pwd1 = "";
    private String pwd2 = "";
    private int flag;//1，忘记提现密码中的修改密码；其他，首次修改密码时设置密码


    public static SetWithDrawPwdFragment newInstance(int flag) {
        SetWithDrawPwdFragment fragment = new SetWithDrawPwdFragment();
        Bundle bundle = new Bundle();
        bundle.putInt("flag", flag);
        fragment.setArguments(bundle);
        return fragment;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_setpwd, container, false);
        ButterKnife.inject(this, viewGroup);
        return super.onCreateView(inflater, viewGroup, savedInstanceState);
    }


    @Override
    protected void initView(View rootView) {
        getBaseActivity().setBackNavigationIcon();
        Bundle bundle = getArguments();
        flag = bundle.getInt("flag");
        if (flag == 1) {
            mTitle1.setText("输入新的提现密码");
            mTitle2.setText("确认新的提现密码");
        } else {
            mTitle1.setText("输入六位提现密码");
            mTitle2.setText("确认六位提现密码");
        }

        mFisrtEyeImg.setOnClickListener(this);
        mSencondEyeImg.setOnClickListener(this);
        mSure.setOnClickListener(this);
        mFirstInputView.setInputCallBack(new InputView.InputCallBack() {
            @Override
            public void onInputFinish(String pwd) {
                pwd1 = pwd;
            }
        });
        mSencondInputView.setInputCallBack(new InputView.InputCallBack() {
            @Override
            public void onInputFinish(String pwd) {
                pwd2 = pwd;
            }
        });

    }

    @Override
    public void loadData() {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.reset(this);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.iv_first_eye:
                if (mFirstInputView.getShowPassword()) {
                    mFisrtEyeImg.setImageResource(R.mipmap.ico_biiyan);
                } else {
                    mFisrtEyeImg.setImageResource(R.mipmap.ico_zhengyan);
                }
                mFirstInputView.changeShow();
                break;
            case R.id.iv_sencond_eye:
                if (mSencondInputView.getShowPassword()) {
                    mSencondEyeImg.setImageResource(R.mipmap.ico_biiyan);
                } else {
                    mSencondEyeImg.setImageResource(R.mipmap.ico_zhengyan);
                }
                mSencondInputView.changeShow();
                break;
            case R.id.tv_sure:
                if (Util.isEmpty(pwd1) || Util.isEmpty(pwd2)) {
                    showTost("请完善密码！", 1);
                    return;
                }
                if (pwd1.equals(pwd2)) {

                    // TODO: 2018/1/18 网络请求
                    setPassWord(pwd1);

                } else {
                    showTost("两次设置的密码不一致！", 1);
                    pwd1 = "";
                    pwd2 = "";
                    mFirstInputView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mFirstInputView.clearResult();
                        }
                    }, 100);
                    mSencondInputView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            mSencondInputView.clearResult();
                        }
                    }, 100);
                    return;
                }
                break;
        }
    }

    private void setPassWord(String pwd1) {
        Apiservice apiservice = ApiUtil.getInstance().create();
        HashMap params = new HashMap();
        params.put("userid", PreferencesUtil.getString(getActivity(), PreferencesUtil.USER_ID));
//        if (flag == 1) {
//            //调用找回密码中的修改提现密码接口
//            params.put("type", 2);
//        } else {
//            //调用首次设置密码接口
        params.put("type", 1);
//        }
        params.put("newPwd", Util.encodeMD5(pwd1));
        showProgress(getClass().getSimpleName());
        Call<ApiResponseBody> call = apiservice.updateNick(params);
        call.enqueue(new Callback<ApiResponseBody>() {
            @Override
            public void onResponse(Call<ApiResponseBody> call, Response<ApiResponseBody> response) {
                dismissProgress();
                if (response.isSuccessful()) {
                    startActivity(new Intent(getActivity(), FindWithDrawSuccessActivity.class));
                    getActivity().finish();
                } else {
                    showTost(response.raw().message(), 1);
                }
            }

            @Override
            public void onFailure(Call<ApiResponseBody> call, Throwable t) {
                dismissProgress();
                ApiException apiException = ExceptionHandle.handleException(t);
                if (apiException.isTokenInvalid()) {
                    showUnLoginSnackbar();
                } else if (apiException.isNetConnected()) {
                    showSetNetworkSnackbar();
                } else {
                    showTost(apiException.getMessage(), 1);
                }
            }
        });

    }


}
